-- Viral Resistance - T-Engine 4 Module
-- Copyright (C) 2010 Mikolai Fajer
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
--
-- Mikolai Fajer "yufra"
-- mfajer@gmail.com

-- Use medicine if health/viral is below/above the wimpy fraction
newAI("use_medicine", function(self)
		for i, item in ipairs(self:getInven("INVEN")) do
			if item.type == "medicine" then
				-- Health meds
				if item.subtype == "health" and self.life / self.max_life < self.ai_state.wimpy then
					item:use(self, "use")
					print("[AI] AI used item", self.uid, self.name, "::", item.uid, item.name)
					return true
				elseif item.subtype == "viral" and self.vload / self.max_vload < (1 - self.ai_state.wimpy) then
					item:use(self, "use")
					print("[AI] AI used item", self.uid, self.name, "::", item.uid, item.name)
					return true
				end
			end
		end
end)

-- AIs for using items
newAI("equip_ammo", function(self)
	-- Check for a gun and a lack of ammo
	local main = self:getInven("MAINHAND")
	local ammo = self:getInven("AMMO")
	if main[1] and main[1].type == "firearm" and (not ammo[1] or ammo[1].subtype ~= main[1].subtype) then
		for i, item in ipairs(self:getInven("INVEN")) do
			if item.type == "ammo" and item.subtype == main[1].subtype then
				self:removeObject(self:getInven(self.INVEN_INVEN), i)
				self:wearObject(item, true)
				self:useEnergy()
				print("[AI] AI equipped item", self.uid, self.name, "::", item.uid, item.name)
				return true
			end
		end
	end
end)